package com.Spring.test_2_3_spring_principle.config;

import com.Spring.test_2_3_spring_principle.model.User;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.context.annotation.ApplicationScope;
import org.springframework.web.context.annotation.RequestScope;
import org.springframework.web.context.annotation.SessionScope;

@Configuration
public class BeanConfig {
    @Bean
    public User user1(){
        return new User(1,"zhangsan");
    }

    // 单例作用域
    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON) // 通过这个标签使用单例模式
    //@Scope("singleton") // 这是第二种写法
    @Bean // 单例作用域, 默认
    public User singleUser(){
        return new User();
    }

    // 多例/原型作用域
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Bean
    public User prototypeUser(){
        return new User();
    }

    // request作用域
    @RequestScope
    @Bean("requestUser") // 指定一下名称
    public User requestUser(){
        return new User();
    }


    // Session 作用域
    @SessionScope
    @Bean("sessionUser")
    public User sessionUser(){
        return new User();
    }


    // application 作用域
    @ApplicationScope
    @Bean("applicationUser")
    public User applicationUser(){
        return new User();
    }

}
